#
# File: Makefile del frammento 2
#

CC=gcc
CFLAGS=-Wall -pedantic
CLIBS= -L./lib -lMainWindow -lncurses -lpthread -lcom
CLIBS1= -L./lib -ltestMainWindow -lncurses -lpthread -lcom

SUBJECT1= "lcschat: Consegna frammento 1"
SUBJECT2= "lcschat: Consegna frammento 2"
SUBJECT3= "lcschat: Consegna finale"

.PHONY: all test1 consegna1  test2 consegna2 test-finale consegna-finale clean cleanall libre

all: mw libre server client testclient

####################(inizio sezione **1**)#####################################
mw: 
	make -C MainWindow/
	cp MainWindow/libMainWindow.a lib/
	make -C stubMainWindow/
	cp stubMainWindow/libMainWindow.a lib/libtestMainWindow.a

server:  server.o lcshash.o
	$(CC) $(CFLAGS) server.o lcshash.o -L./lib -lpthread -lcom -o server

server.o: server.c lcscom.h lcshash.h

#client per test
testclient:  testclient.o
	$(CC) $(CFLAGS) testclient.o  $(CLIBS1)  -o testclient  

testclient.o: client.c lcscom.h MainWindow.h
	$(CC) $(CFLAGS) -c  client.c -o testclient.o

client: client.o
	$(CC) $(CFLAGS) client.o  $(CLIBS)  -o client  

client.o: client.c lcscom.h MainWindow.h
	$(CC) $(CFLAGS) -c client.c

test-frag1: test-frag1.o lcshash.o 

test-frag1.o: lcshash.h

libre: lcscom.o
	ar r libcom.a lcscom.o
	mv libcom.a lib/

lcscom.o: lcscom.c lcscom.h

lcshash.o: lcshash.c lcshash.h

##########################(inizio sezione **2**)#############################

#effettua il test del primo frammento
test1: test-frag1
	cp ./test1/lcshash.dat  ./.lcshash.dat
	cp ./test1/lcshash2.dat ./.lcshash2.dat
	chmod 644 ./.lcshash*
	-rm -f ./.mtrace.log
	@echo MALLOC_TRACE=$(MALLOC_TRACE)
	./test-frag1 1>./output 2>./error
	diff ./output ./test1/output.check
	mtrace ./test-frag1 ./.mtrace.log
	@echo -e "\a\n*** TEST SUPERATO! ***\n"

#effettua il test del primo frammento e la consegna
consegna1:
	make test1
	./gruppo-check.pl < gruppo.txt
	tar -cvf $(USER)-f1.tar ./gruppo.txt ./lcshash.c
	mpack -s $(SUBJECT1) ./$(USER)-f1.tar  susanna@di.unipi.it
	@echo -e "\a\n*** PROGETTO CONSEGNATO! ***\n"

#effettua il test del secondo frammento (con codice studente/i)
test2: 
	-killall server
	-killall client
	-killall testclient
	make all
	wait
	@echo Preparing test environment...
	cp test2/*.test .
	cp test2/*.check .
	chmod 755 *.test 
	chmod 644 *.check
	@echo Starting server process ...
	./server pippo 1>./.serverout 2>./.servererror
	@echo Using checkAll script to make first test ...
	./checkAll . 2>outtest
	more outtest
	diff ./outtest ./test2/outtest.check
	@echo Testing parallelism ...
	./test-frag2
	./checktestfrag2out
	wait
	@echo -e "\a\n*** TEST SUPERATO (2) ! ***\n"


#effettua il test del secondo frammento e la consegna
consegna2:
	make test1
	make test2
	./gruppo-check.pl < gruppo.txt
	tar -cvf $(USER)-f2.tar ./gruppo.txt ./*.h ./*.c ./checkAll ./Makefile
	mpack -s $(SUBJECT2) ./$(USER)-f2.tar  susanna@di.unipi.it
	@echo -e "\a\n*** PROGETTO CONSEGNATO (2) !***\n"

###########################################################################
#effettua il test del progetto completo
test-finale: 
	-killall server
	-killall client
	-killall testclient
	wait
	-rm server
	-rm client
	-rm testclient
	make all
	wait
	@echo Preparing test environment...
	cp test2/*.test .
	cp test2/*.check .
	cp test3/*.check .
	chmod 755 *.test 
	chmod 644 *.check
	@echo Third test ...
	./test-frag3
	cat out3* > outfrag3
	diff outfrag3 outfrag3.check
	wait
	@echo -e "\a\n*** TEST SUPERATO (finale) ! ***\n"


#effettua i test del progetto completo e la consegna
consegna-finale:
	make test1
	make test2
	make test-finale
	./gruppo-check.pl < gruppo.txt
	tar -cvf $(USER)-f3.tar ./gruppo.txt ./*.h ./*.c ./checkAll ./Makefile ./*.pdf ./README.FIRST
	mpack -s $(SUBJECT3) ./$(USER)-f3.tar  susanna@di.unipi.it
	@echo -e "\a\n*** PROGETTO CONSEGNATO (finale) !***\n"

###########################################################################

clean:
	-rm -f *~ core 

cleanall:	
	-rm -f *.o *~ core .mtrace.log 



